import OSS from 'ali-oss';

//
import helper from '@/utils/helper';
import { ststoken } from '@/api';
import { OSSBUCKET, REGION, OSSBASEPATH } from '@/config';

export default {
	objData: {},
	fullPath: '',
	// baseOSSPath:
	// getFullOssPath(imageName){

	//   return OSSBASEPATH + imageName;
	// },

	// baseOSSPath:
	async getFullOssPath(imageName) {
		//add when not login
		if (typeof localStorage == 'undefined') {
			return '';
		}
		//is login
		let strAuthorization = localStorage.getItem('Authorization');
		if (strAuthorization == '') {
			return OSSBASEPATH + imageName;
		}

		await this.stsToken();

		let store = new OSS({
			accessKeyId: this.objData.AccessKeyId,
			accessKeySecret: this.objData.AccessKeySecret,
			bucket: OSSBUCKET,
			// stsToken: this.objData.SecurityToken,
			region: REGION,
		});

		let url = imageName ? store.signatureUrl(imageName) : '';

		return url;

		//return OSSBASEPATH + imageName;
	},

	getSyncUrlFullPath(imageName) {
		let store = new OSS({
			accessKeyId: this.objData.AccessKeyId,
			accessKeySecret: this.objData.AccessKeySecret,
			bucket: OSSBUCKET,
			stsToken: this.objData.SecurityToken,
			region: REGION,
		});

		let url = store.signatureUrl(imageName);

		return url;
	},

	getFullLocationPath() {
		return this.fullPath;
	},

	getFileName() {
		return this.fileName;
	},

	showToast(content, obj) {
		const h = obj.$createElement;
		obj.$message({
			message: h('p', null, [
				h('span', null, content),
				h('i', { style: 'color: teal' }, ''),
			]),
		});
	},

	// 自定义视频上传
	// 回调 0  文件上传成功
	//     -1 文件上传失败
	async ossUploadFile(file, bizName, obj, isPrivate, callback) {
		let errInfo =
			'上传失败:如您多次上传失败,则将文件发邮件至makeable-team@makeable.cn,我们将与您联系! If you upload faild many times, can email the file to makeable-team@makeable.cn ，we will contact you as soon as possible!';
		try {
			await this.stsToken();
		} catch (err) {
			// this.$message({
			//   message: "上传失败！",
			//   type: "warning"
			// });
			this.showToast(errInfo, obj);
			callback(-1);
			return;
		}

		let store = new OSS({
			accessKeyId: this.objData.AccessKeyId,
			accessKeySecret: this.objData.AccessKeySecret,
			bucket: OSSBUCKET,
			stsToken: this.objData.SecurityToken,
			region: REGION,
		});

		let dateTime = helper.dateFormat(new Date(), 'yyyyMMddhhmmss'); // 当前时间

		var ossAcl = {};
		if (isPrivate == false) {
			ossAcl = { 'x-oss-object-acl': 'public-read' };
		}

		//xxl oss
		let uuid = JSON.parse(localStorage.getItem('UserInfo')).uuid;

		this.fullPath =
			'client/' + bizName + '/' + uuid + '/' + dateTime + '_' + file.name;
		store
			.multipartUpload(
				//第一个参数可以设置自定义的路径
				this.fullPath,
				file,
				{
					//获取进度条的值
					progress: (p) => {},
					headers: ossAcl,
				}
			)
			.then(
				//处理成功
				(result) => {
					//处理签名地址
					store.signatureUrl(result.name);
					callback(0);
				}
			)
			.catch((err) => {
				this.showToast(errInfo, obj);
				callback(-1);
			});
	},

	async stsToken() {
		let strAuthorization = localStorage.getItem('Authorization');
		let res = await ststoken(strAuthorization);

		let data = res.data.data;
		//this.stsToken =  data.SecurityToken
		this.objData = data;
	},

	//上传前
	handleVideoBefore(file) {
		const isLt2M = file.size / 1024 / 1024 < 400;
		if (!isLt2M) {
			this.$message({
				message: '上传文件大小不能超过400MB!',
				type: 'warning',
			});
			return false;
		} else {
			return true;
		}
	},
};
